﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>测试页面</title>
	<script type="text/javascript" src="rangy/rangy-core.js"></script>
	<script type="text/javascript">
		var nextid = 0;
		window.onload = function () {
			window.document.execCommand('RespectVisibilityInDesign', true, true);

			document.onmouseup = function () {
				//var selection = rangy.getSelection();
				//var range = selection.getRangeAt(0);
				//range.moveStart("character", -1);
				//var i = range.text.indexOf('\n');
				//return;
				var bigNum = -1e8;
				var selection = document.selection;
				var range = selection.createRange();
				if (range.boundingWidth == 0) {
					//looks like its already at the start of the next line down...
					//console.log('default position: ' + range.offsetLeft + ', ' + range.offsetTop);
					//lets move the start of the range one character back
					//(i.e. select the last char on the line)
					//range.moveStart("character", -1);
					var i = range.text.indexOf('\n');
					//now the range looks good (except that its width will be one char);
					//console.log('one char back: ' + range.offsetLeft + ', ' + range.offsetTop);
					//calculate the true end of the line...
					var clientrect = range.getBoundingClientRect();
					//var clientsrect = range.getClientRects();

					var bookmark = range.getBookmark();
					var testrang = range.duplicate();
					var parent = testrang.parentElement();
					var children = parent.children;
					testrang.moveToElementText(children[children.length - 1]);
					testrang.collapse(false);

					//var bm1 = testrang.getBookmark();
					//range.pasteHTML('-');
					//testrang.pasteHTML('|');
					//					testrang.setEndPoint("EndToStart", range);
					//					var a = testrang.moveStart("character", bigNum);
					//console.log(testrang.isEqual(range));
					var pos = testrang.text.length;



					return;
					if (range.boundingWidth == 0) {
						range.collapse();
						range.select();
					} else {
						var left = range.offsetLeft + range.boundingWidth;
						var top = range.offsetTop;
						//now we can collapse back down to 0 width range
						range.collapse();
						//the position looks right
						//console.log('moving to: ' + left + ', ' + top);
						//move there.
						range.moveToPoint(left, top);
						//oops... on the next line again... stupid IE.
						//console.log('moved to: ' + range.offsetLeft + ', ' + range.offsetTop);
						//range.select();
					}
				}
			};

			document.onkeydown = function () {
				return;
				var evt = arguments[0] || window.event;
				if (evt.keyCode == 13) {
					var sel = document.selection;
					var rang = sel.createRange();
					nextid++;
					rang.pasteHTML('<span data-formattag="' + nextid + '" class="plugin_formattag_br"><img alt="" src="_source/plugins/formattag/images/blank.gif" style="visibility: hidden;" /></span>');
				}
			}


			document.onkeypress = function () {
				var selection = document.selection;
				var range = selection.createRange();
				var test = range.duplicate();
				test.moveStart("character", -1);
				console.log(test.htmlText);
			}
			var text = document.createTextNode("a");
			var text2 = document.createTextNode("");
			var text3 = document.createTextNode("ab");
			document.body.appendChild(text);
			document.body.appendChild(text2);
			document.body.appendChild(text3);
		}
	</script>
	<style type="text/css">
		body { font-family: 宋体,Arial, Verdana, sans-serif; font-size: 14px; padding: 0px; margin: 0px; }
		.plugin_formattag_br { background: transparent url(_source/plugins/formattag/images/br.gif) no-repeat left center; }
	</style>
</head>
<body contenteditable="true">
	aa<br />
	aaa<br />
	aa<br />
	<span class="plugin_formattag_br"><img src="_source/plugins/formattag/images/br.gif" style="visibility:hidden" /></span>
	<p>
		&nbsp;</p>
</body>
</html>
